<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<c:set var="isEmptyList" value="${isEmptyList}" scope="request"/>
<% 
boolean isEmptyList = (Boolean)request.getAttribute("isEmptyList");
if (isEmptyList){
%>

<table cellpadding="3" cellspacing="1" border="0" width="100%" height=500px>
	<tr class="row-title"><td class="header21" align=left>
		&nbsp;<b>error:</b>
	</td></tr>
	<tr><td valign="top" align="center">

		<table cellpadding="0" cellspacing="1" border="0" bgcolor="#4682B4"  width="350"><tr><td>
		<table cellpadding="2" cellspacing="0" border="0" width="350" bgcolor="#DBEAF5">
			<tr><td colspan="2">&nbsp;</td></tr>
			<tr><td align="center">
				<font class="button"><c:out escapeXml="false" value="${message}"/></font>
			</td></tr>
			<tr><td colspan="2">&nbsp;</td></tr>
			</table>
		</table><br>
		
	</td></tr>
</table>

<% }else{ %>

<% 
	int currPageIndex = 1;
	if (request.getParameter("p") != null)
		currPageIndex = Integer.parseInt(request.getParameter("p"));
%>

<table cellpadding="3" cellspacing="1" border="0" width="100%" height=500px>
	<tr class="row-title"><td class="header21" align=left>
		<a href="view.m?page=posts">posts</a>
		<% 
		if (page == null || page.equals("posts-cat")){
		%>
		->&nbsp;<a href="view.m?page=posts-cat&id=<c:out value="${category.id}"/>"><c:out value="${category.name}"/></a>
		<%
		}else if(page.equals("posts-author")){
		%>
		->&nbsp;<a href="user.m?id=<c:out value="${author.id}"/>" ><c:out value="${author.fullname}"/></a>
		<%
		}
		%>
	</td></tr>
	<tr><td valign="top">
		<c:forEach items="${posts}" var="post">

		<table border="0" width=100% cellpadding="2" cellspacing="1">
			<tr class="row-title"><td width=485px align=left class="post-title">
				<a href="view.m?page=post-view&id=<c:out value="${post.id}"/>">
				<b><c:out escapeXml="false" value="${post.title}"/></b></a>
			</td><td align=right class="post-title">
				<fmt:formatDate value="${post.dateTime}" type="both" />
			</td></tr>
			<tr><td colspan="2" align=left>	
				posted by <a href="view.m?page=posts-author&id=<c:out value="${post.author.id}"/>">
				<c:out escapeXml="false" value="${post.author.fullname}"/></a>
				in <a href="view.m?page=posts-cat&id=<c:out value='${post.category.id}'/>" ><c:out value="${post.category.name}"/></a>
			</td></tr>
			
			<c:set var="postImageUrl" value="${post.imageUrl}" scope="request"/>
			<% 
				String postImageUrl = (String)request.getAttribute("postImageUrl");
				if(postImageUrl == null || postImageUrl.trim().equals("")){ 
			%>
				
			<tr><td colspan="2" align=left valign="top">
				<c:out escapeXml="false" value="${post.summary}" />
			</td></tr>
			
			<% }else{ %>

			<tr><td align=left valign="top">
				<c:out escapeXml="false" value="${post.summary}" />
			</td><td align=right>
				<img src="<c:out value="${post.imageUrl}" />" alt="<c:out value="${post.title}"/>" class="image"/>
			</td></tr>
			
			<% } %>
			<tr><td colspan="2" align=left height=20px>
				<a href="view.m?page=post-view&id=<c:out value="${post.id}"/>">more >></a>
			</td></tr>
		</table>
		
		</c:forEach>
	</td></tr>
	<tr><td align="center" valign="top" height=20px>
	<c:set var="numOfPage" value="${numOfPage}" scope="request"/>
	<c:set var="id" value="${id}" scope="request"/>
	<%
		int id = request.getAttribute("id") != null ?
				(Integer)request.getAttribute("id") : 0;
		int numPage = 0;
		if (request.getAttribute("numOfPage") != null){
			numPage = (Integer)request.getAttribute("numOfPage");
		}
		int numVisibleIndex = 5;
		int halfVisibleIndex = numVisibleIndex / 2;
		int startVisibleIndex = currPageIndex - 1 >= halfVisibleIndex ? 
				currPageIndex - halfVisibleIndex : 1;
		int endVisibleIndex = numPage - currPageIndex >= halfVisibleIndex ?
				currPageIndex + halfVisibleIndex : numPage;
		
		out.println("<b>");
		if (currPageIndex > 1){
			out.println("<a href='./view.m?page=" + page + "&id=" + id + "&p=" + (currPageIndex - 1) + "'><</a>&nbsp;");
		}else{
			out.println("<");
		}
		out.println("</b>");
		
		for (int i = startVisibleIndex; i <= endVisibleIndex; i++){
			if (i != currPageIndex)
				out.println("<a href='./view.m?page=" + page + "&id=" + id + "&p=" + i + "'>" + i + "</a>&nbsp;");
			else
				out.println("<b><a href='./view.m?page=" + page + "&id=" + id + "&p=" + i + "'>" + i + "</a></b>&nbsp;");
		}
		
		out.println("<b>");
		if (currPageIndex < numPage){
			out.println("<a href='./view.m?page=" + page + "&id=" + id + "&p=" + (currPageIndex + 1) + "'>></a>");
		}else{
			out.println(">");
		}
		out.println("</b>");
	%>
	</td></tr>	
</table>

<% } %>